package cn.changeforyou.web.cloud.gateway.utils;

import cn.changeforyou.base.model.ResultInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class WebFluxUtils {
    public static byte[] getWrapData(ResultInfo info) {
        return getWrapData(info.getCode(), info.getMessage());
    }

    public static byte[] getWrapData(int code, String message) {
        Map<String, Object> map = buildErrorMap(code, message);
        try {
            return new ObjectMapper().writeValueAsString(map).getBytes();
        } catch (JsonProcessingException e) {
            //
        }
        return "".getBytes();
    }

    public static Map<String, Object> buildErrorMap(int code, String message) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("code", code);
        map.put("message", message);
        return map;
    }

}
